import 'package:flutter/material.dart';
import 'package:module_home/const/string.dart';
import 'package:module_home/model/ISSDataEntity.dart';
import 'package:module_home/model/ScoreEntity.dart';
import 'package:module_home/model/TransferDataEntity.dart';
import 'package:lib_navigator/custom_route.dart';

import 'package:module_home/widget/IssTop.dart';
import 'package:module_home/widget/MenuItem.dart';
import 'package:module_home/widget/ScoreItem.dart';
import 'package:lib_ui/widget/appbar_widget.dart';

import 'Menu_data_page.dart';

///六大区域
class ISSSortPage extends StatefulWidget {
  final List<ScoreEntity> list;

  final List<ISSDataEntity> issDataList;

  const ISSSortPage({Key key, this.list, this.issDataList}) : super(key: key);

  @override
  _ISSSortPageState createState() => _ISSSortPageState();
}

class _ISSSortPageState extends State<ISSSortPage> {
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () => _saveClick(),
        child: Scaffold(
          backgroundColor: Colors.white,
          appBar: appBar(iss_title, toBack: () {
            _saveClick();
          }, actions: <Widget>[
            Padding(
                padding: EdgeInsets.only(right: 15),
                child: Center(
                    child: Container(
                  width: 60,
                  height: 25,
                  child: ElevatedButton(
                    style: ButtonStyle(
                        backgroundColor:
                            MaterialStateProperty.all(Colors.white),
                        shape: MaterialStateProperty.all(StadiumBorder(
                            side: BorderSide(
                          style: BorderStyle.none,
                        )))),
                    child: Text(
                      iss_save,
                      style: TextStyle(color: Colors.blue),
                    ),
                    onPressed: () {
                      _saveClick();
                    },
                  ),
                )))
          ]),
          body: Column(
            children: [
              ISSTop(title: iss_title, img: ""),
              Container(
                  width: double.infinity,
                  height: 48,
                  color: Colors.grey.shade100,
                  child: Row(
                    children: _buildItem(widget.list),
                  )),
              Expanded(
                  child: Container(
                      margin: EdgeInsets.all(20),
                      child: GridView.builder(
                          gridDelegate:
                              SliverGridDelegateWithFixedCrossAxisCount(
                            crossAxisCount: 2,
                            mainAxisSpacing: 10,
                            crossAxisSpacing: 10,
                          ),
                          itemCount: widget.issDataList.length,
                          itemBuilder: (context, index) {
                            return GestureDetector(
                              child: MenuItem(
                                  issDataEntity: widget.issDataList[index]),
                              onTap: () {
                                _toTransferForResult(
                                    context, widget.issDataList[index]);
                              },
                            );
                          }))),
            ],
          ),
        ));
  }

  List<Widget> _buildItem(List<ScoreEntity> items) {
    return items
        .map((item) => Expanded(child: ScoreItem(scoreModel: item)))
        .toList();
  }

  ///跳转的时候我们需要使用异步等待回调结果 dataFromOtherPage 就是返回的结果
  _toTransferForResult(
      BuildContext context, ISSDataEntity iSSDataEntity) async {
    final dataFromOtherPage = await Navigator.push(
      context,
        CustomRoute(
              MenuDataPage(list: widget.list, issDataEntity: iSSDataEntity)),
    ) as TransferDataEntity;

    setState(() {
      // print("六大区域" + dataFromOtherPage.ssValue.toString());
    });
  }

  _saveClick() {
    int AIS = 0;
    widget.issDataList.forEach((element) {
      if (element.aisValue > 0) AIS += element.aisValue;
    });

    widget.issDataList
        .sort((left, right) => right.issValue.compareTo(left.issValue));
    int AIS1 = widget.issDataList.elementAt(0).issValue;
    int AIS2 = widget.issDataList.elementAt(1).issValue;
    int AIS3 = widget.issDataList.elementAt(2).issValue;
    int issValue = AIS1 * AIS1 + AIS2 * AIS2 + AIS3 * AIS3;
    if (AIS1 == 6 || AIS2 == 6 || AIS3 == 6) {
      issValue = 75;
    }
    // print("六大区域" + AIS.toString() + "..." + issValue.toString());
    var transferData = TransferDataEntity(AIS, issValue);
    Navigator.pop(context, transferData);
  }
}
